﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using YaAppoint.PrintTemplates;

namespace YaAppoint.Web.Controllers
{
    public class PrintTemplateController(IPrintTemplateAppService printTemplateAppService) : AbpControllerBase
    {
        private readonly IPrintTemplateAppService _templateAppService = printTemplateAppService;

        /// <summary>
        /// 加载列表
        /// </summary>
        [HttpGet("/api/exam-item/get-template-list")]
        public async Task<IActionResult> LoadTemplateList(Guid depId,Guid deviceTypeId,string? filterName,PrintType? filterPrintType)
        {
            return new JsonResult(await _templateAppService.LoadTemplateList(depId,deviceTypeId,filterName,filterPrintType));
        }

        /// <summary>
        /// 提交新建
        /// </summary>
        [HttpPost("/api/template/submit-create")]
        public async Task<IActionResult> CreateSubmit([FromBody] CreatePrintTemplateDto dto)
        {
            return new JsonResult(await _templateAppService.CreateSubmit(dto));
        }

        /// <summary>
        /// 提交编辑
        /// </summary>
        [HttpPost("/api/template/submit-edit")]
        public async Task<IActionResult> EditSubmit([FromBody]PrintTemplateDto dto)
        {
            return new JsonResult(await  _templateAppService.EditSubmit(dto));
        }

        /// <summary>
        /// 删除模板
        /// </summary>
        [HttpGet("/api/template/delete")]
        public async Task<IActionResult> DeleteTemplate(Guid id)
        {
            return new JsonResult(await _templateAppService.DeleteTemplate(id));
        }

        /// <summary>
        /// 模板详情
        /// </summary>
        [HttpGet("/api/template/get")]
        public async Task<IActionResult> LoadDetail(Guid id)
        {
            return new JsonResult(await _templateAppService.LoadDetail(id));
        }

        [HttpGet("/api/template/get-fields")]
        public async Task<IActionResult> GetTemplateFields(Guid templateId)
        {
            return new JsonResult(await _templateAppService.GetTemplateFields(templateId));
        }

        /// <summary>
        /// 获取FilterPrintType选项
        /// </summary>
        [HttpGet("/api/print-template/get-print-type-options")]
        public IActionResult GetPrintTypeOptions()
        {
            return new JsonResult(_templateAppService.GetPrintTypeOptions());
        }

        /// <summary>
        /// 保存模板JSON
        /// </summary>
        [HttpPost("/api/print-template/save-template-schema")]
        public async Task<IActionResult> SaveTemplateSchema([FromBody]SaveSchemaDto dto)
        {
            return new JsonResult(await _templateAppService.SaveSchema(dto));
        }
    }
}
